"""
基础AI注册服务实现，包含信息抽取、污染类型识别、位置分析等服务
"""
from services.ai_service_factory import ServiceType, ServiceConfig
from core.logging_config import get_logger
from services.visits_services.duplicate_detection_service import DuplicateDetectionService
from services.visits_services.llm_analysis_services import LLMAnalysisService, ExtracInfoService, \
    SentimentAnalysisService, SpecificationAnalysisService, RegionalAnalysisService, \
    ComplaintTypeAnalysisService, DuplicateAnalysisService
from services.visits_services.extract_services import InformationExtractionService
from services.visits_services.location_service import LocationAnalysisService
from services.visits_services.pollution_type_service import PollutionTypeRecognitionService

logger = get_logger("ai_analysis_services")

logger.info("实现服务的注册")


def get_default_service_config(service_type: ServiceType) -> ServiceConfig:
    """获取默认服务配置"""
    configs = {
        ServiceType.INFORMATION_EXTRACTION: ServiceConfig(
            service_type=ServiceType.INFORMATION_EXTRACTION,
            name="信息抽取服务",
            description="从信访文本中提取关键信息",
            version="1.0.0",
            max_concurrent_requests=5,
            timeout=30,
            model_config={
                "model_name": "information-extractor",
                "max_tokens": 1000,
                "temperature": 0.1
            }
        ),
        ServiceType.POLLUTION_TYPE_RECOGNITION: ServiceConfig(
            service_type=ServiceType.POLLUTION_TYPE_RECOGNITION,
            name="污染类型识别服务",
            description="识别信访内容中的污染类型",
            version="1.0.0",
            max_concurrent_requests=5,
            timeout=20,
            model_config={
                "model_name": "pollution-classifier",
                "max_tokens": 500,
                "temperature": 0.2
            }
        ),
        ServiceType.DUPLICATE_DETECTION: ServiceConfig(
            service_type=ServiceType.DUPLICATE_DETECTION,
            name="重复投诉检测服务",
            description="检测重复或相似的投诉内容",
            version="1.0.0",
            max_concurrent_requests=3,
            timeout=60,
            model_config={
                "model_name": "duplicate-detector",
                "similarity_threshold": 0.8,
                "max_tokens": 500
            }
        ),
        ServiceType.SENTIMENT_ANALYSIS: ServiceConfig(
            service_type=ServiceType.SENTIMENT_ANALYSIS,
            name="情感分析服务",
            description="分析投诉内容的情感倾向",
            version="1.0.0",
            max_concurrent_requests=10,
            timeout=15,
            model_config={
                "model_name": "sentiment-analyzer",
                "max_tokens": 200,
                "temperature": 0.1
            }
        ),
        ServiceType.COMPLIANCE_CHECK: ServiceConfig(
            service_type=ServiceType.COMPLIANCE_CHECK,
            name="规则判断服务",
            description="分析信访回复内容是否符合回复的标准格式",
            version="1.0.0",
            max_concurrent_requests=10,
            timeout=15,
            model_config={
                "model_name": "compliance_check",
                "max_tokens": 200,
                "temperature": 0.1
            }
        )
    }

    return configs.get(service_type, ServiceConfig(
        service_type=service_type,
        name=f"{service_type.value}服务",
        description=f"{service_type.value}服务",
        version="1.0.0"
    ))


# 服务注册函数
def register_ai_services():
    """注册AI服务"""
    from services.ai_service_factory import ai_service_factory

    # 注册服务类型
    service_mappings = {
        ServiceType.LLM_SERVICES: LLMAnalysisService,  # 大模型基础服务
        ServiceType.INFORMATION_EXTRACTION: ExtracInfoService,  # 区域分析
        ServiceType.LOCATION_ANALYSIS: LocationAnalysisService,  # 经纬度位置分析服务
        ServiceType.EXTRACT_INFO: InformationExtractionService,  # 区县、街道信息抽取服务
        ServiceType.SENTIMENT_ANALYSIS: SentimentAnalysisService,  ## 情感分析
        ServiceType.COMPLIANCE_CHECK: SpecificationAnalysisService,  ## 规范判断
        # 添加新的分析服务
        # "regional_analysis": RegionalAnalysisService,  # 区域分析服务
        # "complaint_type_analysis": ComplaintTypeAnalysisService,  # 投诉类型分析服务
        # "duplicate_analysis": DuplicateAnalysisService,  # 重复分析服务
        ServiceType.POLLUTION_TYPE_RECOGNITION: PollutionTypeRecognitionService,  # 污染类型识别服务
        # ServiceType.DUPLICATE_DETECTION: DuplicateDetectionService,  # 重复投诉检测服务
    }

    services_to_create = []
    # 遍历所有服务
    for service_type, service_class in service_mappings.items():
        # 注册服务类型（支持枚举和字符串）
        # if isinstance(service_type, str):
        #     # 字符串类型的服务，直接注册
        #     ai_service_factory.service_configs[service_type] = service_class
        #     logger.info(f"注册AI服务类型: {service_type}")
        # else:
        # 枚举类型的服务，使用标准注册方法
        ai_service_factory.register_service_type(service_type, service_class)
        # 搜集所有的服务
        services_to_create.append(service_type)

    # 创建默认服务实例（只创建ServiceType枚举类型的服务）
    for service_type in services_to_create:
        # # 跳过字符串类型的服务，它们不需要默认配置
        # if isinstance(service_type, str):
        #     continue
        try:
            config = get_default_service_config(service_type)
            ai_service_factory.create_service(config)
        except Exception as e:
            logger.warning(f"创建默认服务失败 {service_type}: {str(e)}")
    return ai_service_factory

# 初始化服务注册
ai_service = register_ai_services()